package view;

import connectivity.QueryManager;
import java.awt.Color;
import main.WinkelApplication;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import model.Category;

public class CategoryList extends JPanel implements MouseListener 
{    
    private final Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();     
    private ClassLoader cldr = this.getClass().getClassLoader();   
    
    
    private final int verticalPosition = (dim.height / 12) + 20;
    private final int offset = dim.height / 12;
    private java.net.URL dot = cldr.getResource("view/dot.png");
    private java.net.URL loguit = cldr.getResource("view/cross.png");
    private java.net.URL beheerIcon   = cldr.getResource("view/beheer.png");
    
    public CategoryList() 
    {
        super();
        setLayout(null);
        initComponents();
    }

    /** create the gui for this page */
    private void initComponents() 
    {
        addTitle(dim.width, dim.height);
        addcategoryItems();
        addBasket();
        addBeheer();
        addLogOut();
    }

    private void addTitle(int width, int height) 
    {
        JLabel lblTitle1 = new JLabel("Winkelapplicatie",SwingConstants.CENTER);
        lblTitle1.setBounds(20, 20, (dim.width - ((dim.width/5)+40)), dim.height/12);
        lblTitle1.setFont(WinkelApplication.computeFontSize(1));
        lblTitle1.setName("-1");
        
        add(lblTitle1);
    }
    
    private void addBeheer() 
    {
        ImageIcon icon = new ImageIcon(beheerIcon);
        
        JLabel beheer = new JLabel(icon);
        beheer.setName("-2");
        beheer.setBounds(20, dim.height - 80, 48, 48);
        beheer.addMouseListener(this);
        
        add(beheer);
    }

    /** add the different catergories to the page */
    private void addcategoryItems()  
    {
        QueryManager queryManager = WinkelApplication.getQueryManager();
        List<Category> categories = queryManager.getCategories();
        
        for (int i = 0; i < categories.size(); i++) 
        {    
            Category category = categories.get(i);

            ImageIcon icon = new ImageIcon(dot);
            
            JLabel lblDot = new JLabel(icon);
            lblDot.setBounds(20, verticalPosition + i * offset, 16, dim.height/24);
            add(lblDot);

            JLabel lblCategorie = new JLabel();
            lblCategorie.setBackground(new Color(151, 186, 225));
            lblCategorie.setName(String.valueOf(category.getCategoryId()));
            lblCategorie.setText(category.getName());
            lblCategorie.setBounds(40, verticalPosition + i * offset, dim.width - ((dim.width/3)), dim.height/24);
            lblCategorie.setFont(WinkelApplication.computeFontSize(2));
            lblCategorie.addMouseListener(this);
            lblCategorie.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            add(lblCategorie);

            JLabel lblDescription = new JLabel();
            lblDescription.setText(category.getDescription());
            lblDescription.setBounds(40, verticalPosition  + i * offset + dim.height/24, dim.width - ((dim.width/5)+80), dim.height/24);
            lblDescription.setFont(WinkelApplication.computeFontSize(3));
            add(lblDescription);
        }
    }
    
    private void addLogOut() 
    {
        ImageIcon icon = new ImageIcon(loguit);
        
        JLabel lblTitle1 = new JLabel("Log Uit", icon, SwingConstants.LEFT);
        lblTitle1.setLayout(null);
        lblTitle1.setBounds(78, dim.height-52, 100 , 20);
        //20, dim.height - 80, 48, 48
        lblTitle1.setName("-3");
        lblTitle1.addMouseListener(this);
        
        add(lblTitle1);
    }

    /** add the basket view into this page */
    private void addBasket() 
    {
        add(new BasketDisplay());
    }

    @Override
    public void mouseClicked(MouseEvent event) 
    {
        JLabel label = (JLabel) event.getSource();
        int categoryId = Integer.parseInt(label.getName());
        
        WinkelApplication.getInstance().setLastCategory(categoryId);
        
        if (label.getName().equals("-2"))
           WinkelApplication.getInstance().showPanel(new view.Manage(WinkelApplication.getInstance().getAccesLevel())); 
        else if (label.getName().equals("-3"))
            WinkelApplication.getInstance().showPanel(new view.LogInScreen());
        else
            WinkelApplication.getInstance().showPanel(new ProductList(categoryId));
    }
    
    @Override
    public void mouseExited(MouseEvent event) 
    {
        JLabel label = (JLabel) event.getSource();
        label.setBackground(new Color(151, 186, 225));
        label.setForeground(Color.black);
    }

    public void mouseEntered(MouseEvent event)
    {
        JLabel label = (JLabel) event.getSource();
        label.setBackground(new Color(151, 186, 225));
        label.setForeground(Color.blue);
    }

    public void mousePressed(MouseEvent me)
    {
        // Nothing to do..
    }

    public void mouseReleased(MouseEvent me)
    {
        // Nothing to do..
    }
}
